package fr.emse.tatiana.analysis.annotation.categorisation;

import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;

import fr.emse.tatiana.analysis.annotation.AnnotationAnalysisEditor;


public class CategorisationAnalysisEditor extends AnnotationAnalysisEditor {
	
	public static final String ID = "fr.emse.tatiana.CategorisationAnalysisEditor";

	
	/* (non-Javadoc)
	 * @see fr.emse.tatiana.replayable.analysis.AnalysisEditor#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
	 */
	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		super.init(site, input);
		if (!(model instanceof CategorisationAnalysis)) {
			throw new PartInitException("Expected CategorisationAnalysis");
		}
	}


	/* (non-Javadoc)
	 * @see fr.emse.tatiana.replayable.analysis.AnalysisEditor#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createPartControl(Composite parent) {
		super.createPartControl(parent);
		Composite c = new Composite(parent, SWT.BORDER);
		GridLayout gl = new GridLayout();
		gl.numColumns=4;
		c.setLayout(gl);
		Button addline = new Button(c,SWT.PUSH);
		addline.setText("Add Category");
		addline.addSelectionListener(new SelectionListener(){

			public void widgetDefaultSelected(SelectionEvent e) {
				// doesn't happen	
			}

			public void widgetSelected(SelectionEvent e) {
				addLine();
			}
			
		});
		Button removeline = new Button(c,SWT.PUSH);
		removeline.setText("Remove Category");
		removeline.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent e) {
				// doesn't happen
				
			}

			public void widgetSelected(SelectionEvent e) {
				removeLine();
			}
			
		});
		final Label catfile = new Label(c,SWT.NONE);
		catfile.setText(((CategorisationAnalysis)model).getCategorisationFileName());
		Button savecategorisation = new Button(c, SWT.PUSH);
		savecategorisation.setText("Save Categories As...");
		savecategorisation.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent e) {
				// never used
				
			}

			public void widgetSelected(SelectionEvent e) {
				InputDialog dialog = new InputDialog(getSite().getShell(), 
						"Save Categorisation As", 
						"Name of Categorisation:", null, null); 
				if (dialog.open() != Window.OK || dialog.getValue().equals(""))
					return;
				String value = dialog.getValue();
				if (!value.endsWith(".xml")) {
					value += ".xml";
				}
				((CategorisationAnalysis)model).saveCategFileAs(value);
				catfile.setText(((CategorisationAnalysis)model).getCategorisationFileName());
			}
			
		});
	}

	protected void addLine() {
		InputDialog dialog = new InputDialog(this.getSite().getShell(), 
				"New Category", 
				"Name of new category:", null, null); 
		if (dialog.open() != Window.OK || dialog.getValue().equals(""))
			return;
		((CategorisationAnalysis) model).addCategory(dialog.getValue());
	}
	
	protected void removeLine() {
		IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
		if (sel.isEmpty())
			return;
		((CategorisationAnalysis) model).removeCategory(sel);
	}

	
	
}
